package pro.lepers.portfolio.view.misc {
	import pro.lepers.portfolio.Consts;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * @author plepers
	 */
	public class Background extends Sprite {

		
		private static const BASE_W : Number = 1000;
		private static const BASE_H : Number = 700;

		public function Background () {
			_build( );
			
		}


		public function setSize ( w : int, h : int ) : void {
			
			_h = h;
			_w = w;
			
			_invalidate( );
		}

		
		public function get upscale () : Boolean {
			return _upscale;
		}

		public function set upscale (upscale : Boolean) : void {
			_upscale = upscale;
//			
//			if( _upscale ) {
//				resultBmp.x = bas
//			}
		}

		
		
		private function _build () : void {
			
			drawableContainer = new Sprite( );
			
			_noisePat = new BitmapData( 256, 256, false, 0 );
			_noisePat.noise( 54634, 0, 20, 7, true );
			
//			_noisePat = new PaperNoise( 512, 512, true, 0 );
			
			_baseShape = new BGShapeContainer_FC( );
			drawableContainer.addChild( _baseShape );
			
			_noiseShape = new Shape( );
			_noiseShape.blendMode = BlendMode.ADD;
			drawableContainer.addChild( _noiseShape );
			
			resultBmp = new Bitmap( );
			resultBmp.smoothing = true;
			addChild( resultBmp );
		}

		private function _redraw ( e : Event = null ) : void {
			if( resultBmp.bitmapData )
				resultBmp.bitmapData.dispose( );
				
			var invScale : Number = 1 / Consts.SCALE_FX;


			_noiseShape.graphics.clear( );
			_noiseShape.graphics.beginBitmapFill( _noisePat );
			_noiseShape.graphics.drawRect( 0, 0, _w * invScale, _h * invScale );
			
			_baseShape.scaleX = _w / BASE_W;
			_baseShape.scaleY = _h / BASE_H;
			_baseShape.x = -_w * ( 1 - invScale ) / 2;
			_baseShape.y = -_h * ( 1 - invScale ) / 2;
			//			var transMatrix : Matrix = new Matrix( );
			//			transMatrix.translate( , BASE_W * invScale/2 );	

			resultBmp.bitmapData = new BitmapData( _w * invScale, _h * invScale, false );
			resultBmp.bitmapData.draw( drawableContainer );
			
			var dsc : Number = (1 - 1 / Consts.SCALE_FX);
			resultBmp.x = (_w / 2) * dsc;
			resultBmp.y = (_h / 2) * dsc;
			
			_valid = true;
			stage.removeEventListener( Event.RENDER, _redraw );
			removeEventListener( Event.ADDED_TO_STAGE, _redraw );
		}

		private function _invalidate () : void {
			
			//			_redraw();
			//			return;
			if( !_valid ) return;
			if( stage ) {
				stage.addEventListener( Event.RENDER, _redraw );
				stage.invalidate( );
			} else 
				addEventListener( Event.ADDED_TO_STAGE, _redraw );
			
			_valid = false;
		}

		
		
		private var _baseShape : Sprite;

		private var _noiseShape : Shape;

		private var _noisePat : BitmapData;

		private var drawableContainer : Sprite;

		private var resultBmp : Bitmap;

		private var _valid : Boolean = true;

		private var _upscale : Boolean = false;


		
		
		private var _w : int;
		private var _h : int;
	}
}
